<!--********************************************************************
* Copyright© 2000 - 2018 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>

<head>
    <meta charset='utf-8' />
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
    <title data-i18n="resources.title_drawControlWGS84"></title>
    <script type="text/javascript" src="../js/include-web.js"></script>
    <script type="text/javascript" include="mapbox-gl-enhance,draw" src="../../dist/mapboxgl/include-mapboxgl.js"></script>
    <style>
        body {
            margin: 0;
            padding: 0;
        }

        #map {
            position: absolute;
            top: 0;
            bottom: 0;
            width: 100%;
        }
    </style>
</head>

<body>
    <div id='map'></div>
    <script type="text/javascript">
        var host = window.isLocal ? window.server : "http://support.supermap.com.cn:8090";
        var count = -1;
        var map = new mapboxgl.Map({
            container: 'map', 
            style: {
                "version": 8,
                "sources": {
                    "raster-tiles": {
                        "type": "raster",
                        "tiles": [host + '/iserver/services/map-world/rest/maps/World'],
                        "rasterSource":"iserver",
                        "tileSize": 256
                    }
                },

                "layers": [{
                    "id": "simple-tiles",
                    "type": "raster",
                    "source": "raster-tiles",
                    "minzoom": 0,
                    "maxzoom": 22
                }]
            },
            crs: mapboxgl.CRS.EPSG4326,
            center: [0, 0],
            zoom: 2
        });
        map.on('load', function () {
            draw = new MapboxDraw({
                displayControlsDefault: false,
                controls: {
                    polygon: true,
                    trash: true
                }
            });
            map.addControl(draw, 'top-left');
            map.on('draw.create', getFeature);

            function getFeature(e) {
                if (map.getLayer("queryDatas")) {
                    map.removeLayer("queryDatas");
                }
                if (map.getSource("queryDatas")) {
                    map.removeSource("queryDatas");
                }

                var data = draw.getAll();
                var feature = data.features[data.features.length - 1];
                var geometryParam = new SuperMap.GetFeaturesByGeometryParameters({
                    datasetNames: ["World:Countries"],
                    geometry: feature,
                    spatialQueryMode: "INTERSECT"
                });
                new mapboxgl.supermap.FeatureService(
                    host + "/iserver/services/data-world/rest/data").getFeaturesByGeometry(
                    geometryParam,
                    function (
                        serviceResult) {
                        map.addSource("queryDatas", {
                            "type": "geojson",
                            "data": serviceResult.result.features
                        });
                        map.addLayer({
                            "id": "queryDatas",
                            "type": "fill",
                            "source": "queryDatas",
                            "paint": {
                                "fill-color": "rgba(255, 251, 240, 0.4)",
                                'fill-outline-color': "#ff0000",
                            },
                        });
                    });
            }
            map.addControl(new mapboxgl.NavigationControl());

        });

    </script>

</body>

</html>
